import App from './App'

// #ifndef VUE3
import Vue from 'vue'
import store from './store'
import share from '@/common/share.js'
Vue.mixin(share);
import request from '@/common/request.js'
Vue.prototype.$request = request;
import * as navTo from '@/common/navto.js';
Vue.prototype.$navto = navTo;
Vue.config.productionTip = false


// main.js


// 根据环境设置域名
let baseUrl = '';
if (process.env.NODE_ENV === 'development') {
	baseUrl = 'https://juanjiahui.guguan.net/'; // 开发环境域名
} else {
	baseUrl = 'https://juanjiahui.guguan.net/'; // 生产环境域名
}
Vue.prototype.$baseUrl = baseUrl;

Vue.prototype.$toMini = function(appId, path = '', extraData = {}) {
	uni.navigateToMiniProgram({
		appId: appId,
		path: path,
		extraData: extraData,
		success(res) {
			console.log('跳转成功', res);
		},
		fail(err) {
			console.error('跳转失败', err);
		}
	});
}

Vue.prototype.$toast = function(str) {
	uni.showToast({
		title: str,
		icon: 'none',
		duration: 1000
	});
}
Vue.prototype.$back = function(num) {
	uni.navigateBack({
		delta: num
	});
}
Vue.prototype.$switchTab = function(url) {
	uni.switchTab({
		url: url,
		success: () => {
			console.log('跳转成功');
		},
		fail: (err) => {
			console.error('跳转失败', err);
		}
	});
}

Vue.prototype.$add = function(num) {
	this.num++;
}
Vue.prototype.$reduce = function(num) {
	if (this.num > 1) {
		this.num--;
	}
}
Vue.prototype.$getFormattedMonths = function() {
	const currentDate = new Date();
	const currentMonth = currentDate.getMonth(); // 获取当前月份（0-11）
	const nextDate = new Date(currentDate);
	nextDate.setMonth(currentDate.getMonth() + 1); // 设置为下个月
	const nextMonth = nextDate.getMonth(); // 获取下个月份（0-11）
	// 月份名称映射
	const monthNames = [
		"1月", "2月", "3月", "4月", "5月", "6月",
		"7月", "8月", "9月", "10月", "11月", "12月"
	];
	// 生成目标格式的月份数组
	const months = [{
			value: 0,
			text: monthNames[currentMonth]
		},
		{
			value: 1,
			text: monthNames[nextMonth]
		}
	];
	return months;
}

Vue.prototype.$getDateRanges = function() {
	const today = new Date();
	const year = today.getFullYear();
	const month = today.getMonth() + 1;
	const lastDayCurrentMonth = new Date(year, month, 0).getDate();
	const currentMonthDates = [];
	for (let day = today.getDate(); day <= lastDayCurrentMonth; day++) {
		currentMonthDates.push(day);
	}
	const nextMonth = month === 12 ? 1 : month + 1;
	const nextYear = month === 12 ? year + 1 : year;
	const lastDayNextMonth = new Date(nextYear, nextMonth, 0).getDate();
	const nextMonthDates = [];
	for (let day = 1; day <= lastDayNextMonth; day++) {
		nextMonthDates.push(day);
	}
	return {
		currentMonthDates,
		nextMonthDates
	};
}


Vue.prototype.$formatMonthString = function(monthString) {
	// 去掉后面的“月”
	const monthNumber = parseInt(monthString, 10);
	if (isNaN(monthNumber)) {
		return '输入格式错误';
	}
	// 补0处理
	const formattedMonth = monthNumber < 10 ? '0' + monthNumber : monthNumber.toString();
	return formattedMonth;
}

Vue.prototype.$getCurrentAndNextMonthYear = function() {
	const today = new Date();
	const currentYear = today.getFullYear();
	const currentMonth = today.getMonth() + 1; // 月份从 0 开始，需要加 1
	let nextYear = currentYear;
	let nextMonth = currentMonth + 1;
	if (nextMonth > 12) {
		nextMonth = 1;
		nextYear++;
	}
	const currentMonthYear = `${currentYear}-${currentMonth.toString().padStart(2, '0')}`;
	const nextMonthYear = `${nextYear}-${nextMonth.toString().padStart(2, '0')}`;
	return {
		currentMonthYear,
		nextMonthYear
	};
}


Vue.prototype.$today = function() {
	// 创建一个 Date 对象
	const today = new Date();
	// 获取年、月、日
	const year = today.getFullYear();
	const month = today.getMonth() + 1; // 月份从 0 开始，需要加 1
	const day = today.getDate();
	// 将月份和日期转换为字符串，并在数字小于 10 时前面补 0
	const monthStr = month < 10 ? '0' + month : month.toString();
	const dayStr = day < 10 ? '0' + day : day.toString();
	// 创建一个数组，将年、月、日放入数组中
	const dateArray = [year, monthStr, dayStr];
	return dateArray;
	console.log('当天日期数组：', dateArray);
}

Vue.prototype.$formatDateString = function(dateString) {
	if (!dateString) return ''; // 如果 dateStr 为空，返回空字符串
	const dateArray = dateString.split('-');
	const month = dateArray[1];
	const day = dateArray[2].split(' ')[0];
	return `${month}/${day}`;
}
Vue.prototype.$formDate = function() {
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	month = (month > 9) ? month : ("0" + month);
	day = (day < 10) ? ("0" + day) : day;
	var today = year + "-" + month + "-" + day;
	return today;
}
Vue.prototype.$timestampToTime = function(time, includeTime = false) {
    // 判断时间戳是10位还是13位
    if (typeof time === 'number' || typeof time === 'string') {
        if (time.toString().length === 10) {
            // 10位时间戳（秒级），转换为毫秒级
            time = parseInt(time) * 1000;
        } else if (time.toString().length === 13) {
            // 13位时间戳（毫秒级），直接使用
            time = parseInt(time);
        } else {
            // console.error('时间戳格式错误，必须是10位或13位数字');
            return '';
        }
    } else {
        // console.error('时间戳必须是数字或字符串');
        return '';
    }

    // 转换为日期对象
    let date = new Date(time);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;

    // 根据第二个参数决定返回格式
    if (includeTime) {
        return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s; // 包含时间
    } else {
        return y + '-' + MM + '-' + d; // 仅返回日期
    }
};
Vue.prototype.$dateToTimestamp = function(dateStr) {
	var timestamp = Date.parse(dateStr);
	return timestamp / 1000; // 将毫秒级别的时间戳转换为秒
}
//电话转换
Vue.prototype.$phone = function(phone) {
	// let reg = /^(\d{3})\d{4}(\d{4})$/
	// let result = phone.replace(reg, '$1****$2')
	const phoneNumber = "phone";
	const maskedPhoneNumber = phoneNumber.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
	console.log(maskedPhoneNumber);
	return maskedPhoneNumber;
}
//字符串截取前几位
Vue.prototype.$jiequ = function(str, num) {
	let substr = str.slice(0, num);
	return substr;
}
//日期转时间戳
Vue.prototype.$shijiancuo = function(dateStr) {
	var timestamp = Date.parse(dateStr);
	return timestamp; // 将毫秒级别的时间戳转换为秒
}
// 将13位时间戳转换为日期
Vue.prototype.$riqi1 = function(timestamp) {
	var date = new Date(timestamp); // 将时间戳转换为毫秒
	var year = date.getFullYear();
	var month = ('0' + (date.getMonth() + 1)).slice(-2);
	var day = ('0' + date.getDate()).slice(-2);
	return year + '-' + month + '-' + day;
}
Vue.prototype.$year = function(timestamp) {
	var date = new Date(timestamp); // 将时间戳转换为毫秒
	var year = date.getFullYear();
	var month = ('0' + (date.getMonth() + 1)).slice(-2);
	var day = ('0' + date.getDate()).slice(-2);
	return year;
}
App.mpType = 'app'
const app = new Vue({
	store, // 挂载 store
	...App
})
app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif